//
//  POISearchViewController.m
//  OCUI
//
//  Created by login on 15-7-29.
//
//

#import "POISearchViewController.h"

#import "HttpClient.h"
#import "Header.h"
#import "SBJSON.h"
@interface POISearchViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
    UITextField *inputSearch;
    UITableView *poiTableView;
}
@property(nonatomic,copy)NSArray *dataArray;
@end

@implementation POISearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    

    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 40)];
    headerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:headerView];
    
    UIButton *turn = [[UIButton alloc]initWithFrame:CGRectMake(10, 0, 50, 40)];
    [turn setTitle:@"返回" forState:UIControlStateNormal];
//    [turn addTarget:self action:@selector(returnHomeView) forControlEvents:UIControlEventTouchUpInside];
    [turn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    turn.titleLabel.font = [UIFont systemFontOfSize:20];
    [headerView addSubview:turn];

    
    inputSearch = [[UITextField alloc]initWithFrame:CGRectMake( 80 , 5, self.view.frame.size.width - 100, 30)];
    inputSearch.placeholder = @"请输入出发地点";
    inputSearch.delegate = self;
    inputSearch.backgroundColor = [UIColor whiteColor];
    inputSearch.borderStyle = UITextBorderStyleRoundedRect;
    inputSearch.clearButtonMode = UITextFieldViewModeWhileEditing;
    [headerView addSubview:inputSearch];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(POISearchCityName) name:UITextFieldTextDidChangeNotification object:nil];

    poiTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 104, self.view.frame.size.width, self.view.frame.size.height-104) style:UITableViewStylePlain];
    poiTableView.delegate = self;
    poiTableView.dataSource = self;
    poiTableView.hidden = YES;
    [self.view addSubview:poiTableView];

    
}


- (void)POIsearch:(NSString *)textFiled
{
    NSString *hostString = @"http://api.map.baidu.com/place/v2/suggestion";
    NSString *urlString = [NSString stringWithFormat:@"%@?query=%@&region=131&output=json&ak=vMVqyC3prGmBgEtjx9ssb31W&mcode=cn.oc.ui",hostString,textFiled];
    NSString *requestString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:requestString];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (!connectionError) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&connectionError];
            [self performSelectorOnMainThread:@selector(dddddddd:) withObject:dic waitUntilDone:YES];
            
        }else{
            NSLog(@"connectionError----%@",connectionError);
        }
        
    }];
    NSLog(@"urlString-----%@",urlString);

//    NSString *hostString = @"http://api.map.baidu.com/place/v2/suggestion";
//    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//    [dict setObject:@"天安门" forKey:@"query"];
//    [dict setObject:@"131" forKey:@"region"];
//    [dict setObject:@"json" forKey:@"output"];
//    [dict setObject:@"vMVqyC3prGmBgEtjx9ssb31W" forKey:@"ak"];
//    [dict setObject:@"cn.oc.ui" forKey:@"mcode"];
//
}
- (void)dddddddd:(NSDictionary *)dicty
{

    self.dataArray = dicty[@"result"];
    [poiTableView reloadData];
    
    NSLog(@"dataArray-----%@",self.dataArray);
    
}

#pragma mark - 收回键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)POISearchCityName
{
    NSLog(@"inputSearch.text----%@",inputSearch.text);
    
    [self POIsearch:inputSearch.text];
    poiTableView.hidden = NO;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    if (self.dataArray.count != 0) {
        cell.textLabel.text = self.dataArray[indexPath.row][@"name"];
        NSString *city = self.dataArray[indexPath.row][@"city"];
        NSString *district = self.dataArray[indexPath.row][@"district"];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@%@",city,district];
    }else
    {
        poiTableView.hidden = YES;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    inputSearch.text = self.dataArray[indexPath.row][@"name"];
    poiTableView.hidden = YES;
    [inputSearch resignFirstResponder];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
